package com.paper.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.paper.entity.Paper;
import com.paper.entity.dto.PaperTableDTO;
import com.paper.service.IPaperService;
import com.test.entity.dto.TestJson;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/paper")
public class PaperController {
	@Autowired
	private IPaperService paperService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<PaperTableDTO> findPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			return paperService.findPage(PaperTableDTO.getSpecification(ids,str,subject_id), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(paperService.isUsing(id))
				return new AjaxResponse(false, "试卷已被使用，不允许删除!");
			paperService.delete(id);
			return new AjaxResponse(true, "已删除!");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败!");
		}
	}
	@RequestMapping("/save")
	public @ResponseBody AjaxResponse save(String paper,HttpSession session) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Paper obj = objectMapper.readValue(paper,Paper.class);
			if(obj.getId()!=null&&paperService.isUsingAndExaming(obj.getId()))
				return new AjaxResponse(false, "该试卷已被使用，且考试已开始或结束，不允许修改!");
			paperService.save(obj, session);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/findAll")
	public @ResponseBody List<Paper> findAll() {
		try {
			return paperService.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Paper findOne(Long id) {
		try {
			
			Paper paper = paperService.findOne(id);
			System.out.println(paper);
			return paper;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
	}
	@RequestMapping("/findListBycourse")
	public @ResponseBody List<Paper> findListBycourse(Long course_id){
		try {
			return paperService.findListBycourse(course_id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
}
